package com.lqian.simplestructure.queue;

import com.lqian.simplestructure.stack.MyStack;

import java.util.Arrays;

public class MyQueue {
    private int[] elements;

    public MyQueue() {
        this.elements = new int[0];
    }

    //入队
    public void  add(int element){
        int[] newArr = new int[elements.length + 1];

        for (int i = 0;i < elements.length;i++){
            newArr[i] = elements[i];
        }
        newArr[newArr.length - 1] = element;

        elements = newArr;
    }

    //出队
    public int poll(){
        if (elements.length == 0){
            throw new RuntimeException("Queue is empty");
        }
        int element = elements[0];
        int[] newArr = new int[elements.length - 1];
        for (int i=0;i< elements.length -1;i++){
                newArr[i] = elements[i+1];
        }
        elements = newArr;
        return element;
    }

    //查看栈内的元素
    public void show(){
        System.out.println(Arrays.toString(elements));
    }

    //队列是否为空
    public boolean isEmpty(){
        return elements.length == 0;
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();

        myQueue.add(11);
        myQueue.add(22);
        myQueue.add(33);
        myQueue.show();
        myQueue.poll();
        myQueue.show();
        System.out.println(myQueue.isEmpty());
        myQueue.show();

        myQueue.poll();
        myQueue.poll();
        System.out.println(myQueue.isEmpty());
    }
}
